﻿//hash改变后触发
function changePage(){
    var actionName = window.location.hash.replace('#', '');
    actionName = actionName.split('&');
    if(actionName[0] == 'add'){
        $('#wst-page1').addClass('wst-page1');
        $('#wst-page2').show();
        toAddNewAccount();
    }else if(actionName[0] == 'edit'){
        $('#wst-page1').addClass('wst-page1');
        $('#wst-page2').show();
        toEditAccount(actionName[1]);
    }else if(actionName[0] == 'withdraw'){
        $('#wst-page1').addClass('wst-page1');
        $('#wst-page2').show();
        toWithdraw();
    }else{
        $('#wst-page1').show();
        $('#wst-page2').addClass('wst-page2');
        setTimeout(function(){
            $('body').removeClass('ajaxpage-active');
        },100);
        setTimeout(function(){
        	$('#wst-page1').removeClass('wst-page1');
            $('#wst-page2').css('display','none');
        },500);
    }
}

//去提现
function toWithdraw(){
    $('#wst-default-loading').modal();
    $.post(WST.U('WebApp/Money/getAccount'), {}, function(data){
        var json = WST.toJson(data);
        var template = Handlebars.compile( $('#withdraw').text() );
        var html = template({'accounts': json});
        $('#wst-page2').html(html);
        $('#wst-default-loading').modal('close');
        $('body').addClass('ajaxpage-active');
        setTimeout(function(){ 
            $('#wst-page1').hide();
            $('#wst-page2').removeClass('wst-page2');
        }, 300);
        json = template = html = null;
    });
}

//提现
function withdraw(){
    var configId = $('#configId').val();
    var money = $('#money').val();
    var userMoney = Number( $('#userMoney').html() );
    var payPwd = $('#payPwd').val();
    if(configId == ''){
        wstMsg('请选择提现账号', 'configId');
        return false;
    }
    if(money == ''){
        wstMsg('请输入提现金额', 'money');
        return false;
    }
    var accNoReg = /[0-9]*/;
    if( !accNoReg.test(money) ){
        wstMsg('金额格式错误', 'money');
        return false;
    }
    if( Number(money) > userMoney ){
        wstMsg('提现金额不能大于现有金额', 'money');
        return false;
    }
    if(payPwd == ''){
        wstMsg('请输入支付密码', 'payPwd');
        return false;
    }

    var post = {
        'configId': configId,
        'drawMoney': money,
        'payPwd': payPwd
    };
    $.post(WST.U('WebApp/CashDraws/drawsCash'), post, function(data){
        var json = WST.toJson(data);
        if(json.status == 1){
            wstMsg('提交成功');
            setTimeout(function(){
                location.href = WST.U('WebApp/Money/index');
            }, 2000);
        }else{
            wstMsg(json.msg);
        }
    });
}

//新增提现账户页
function toAddNewAccount(){
    var template = Handlebars.compile( $('#editAccount').text() );
    var html = template('');
    $('#wst-page2').html(html);
    $('body').addClass('ajaxpage-active');
    setTimeout(function(){ 
        $('#wst-page1').hide();
        $('#wst-page2').removeClass('wst-page2');
    }, 300);
    getBankList();
    getProvinceList();
    template = html = null;
}

//编辑提现账户页
function toEditAccount(id){
    $('#wst-default-loading').modal();
    $.post(WST.U('WebApp/Money/getAccountDetails'), {id: id}, function(data){
        var json = WST.toJson(data);
        var template = Handlebars.compile( $('#editAccount').text() );
        var html = template(json);
        $('#wst-page2').html(html);
        $('#wst-default-loading').modal('close');
        $('body').addClass('ajaxpage-active');
        setTimeout(function(){ 
            $('#wst-page1').hide();
            $('#wst-page2').removeClass('wst-page2');
        }, 300);
        getBankList(json.accTargetId);
        getProvinceList(json.areaId1, json.areaId2);
        json = template = html = null;
    });
}

//获取银行列表
function getBankList(accTargetId){
    $.post(WST.U('WebApp/Money/getBankList'), {}, function(data){//获取省份列表
        var json = WST.toJson(data);
        var str = '';
        if(json.length && json.length > 0){
            for(var i=0; i<json.length; i++){
                str += '<option value="'+ json[i].bankId +'" ';
                str += (json[i].bankId == accTargetId) ? 'selected' : '';
                str += '>' + json[i].bankName +'</option>';
            }
            $('#accTargetId').append(str);
        }
        data = json = str = null;
    });
}

//获取省份列表
function getProvinceList(areaId1, areaId2){
    $.post(WST.U('WebApp/Areas/getProvinceList'), {}, function(data){//获取省份列表
        var json = WST.toJson(data);
        var str = '';
        if(json.status==1 && json.list){
            for(var i in json.list){
                str += '<option value="'+ json.list[i].areaId +'" ';
                str += (json.list[i].areaId == areaId1) ? 'selected' : '';
                str += '>' + json.list[i].areaName +'</option>';
            }
            $('#provice').append(str);
        }
        data = json = str = null;
        if(areaId1 && areaId2){
            getCitysByProvince(areaId1, areaId2);
        }
    });
}

//根据省份获取城市列表
function getCitysByProvince(provinceId, areaId2){
    $('#city').html('<option value="">请选择</option>');
    $.post(WST.U('WebApp/Areas/getCityListByProvince'), {provinceId:provinceId}, function(data){
        var json = WST.toJson(data);
        var str = '';
        if(json.length>0){
            for(var i=0; i<json.length; i++){
                str += '<option value="'+ json[i].areaId +'" ';
                str += (json[i].areaId == areaId2) ? 'selected' : '';
                str += '>'+ json[i].areaName +'</option>';
            }
        }
        $('#city').append(str);
        data = json = null;
    });
}

//保存银行账户
function saveAccount(id){
    var accTargetId = $('#accTargetId').val();
    var areaId2 = $('#city').val();
    var accUser = $('#accUser').val();
    var accNo = $('#accNo').val();
    if(accTargetId == ''){
        wstMsg('请选择银行卡所属银行', 'accTargetId');
        return false;
    }
    if(areaId2 == ''){
        wstMsg('请选择开户地址');
        return false;
    }
    if(accUser == ''){
        wstMsg('请输入持卡人姓名', 'accUser');
        return false;
    }
    if(accNo == ''){
        wstMsg('请输入卡号', 'accNo');
        return false;
    }
    var accNoReg = /[0-9]{16,19}/;
    if( accNo.length < 16 || accNo.length > 19 || !accNoReg.test(accNo) ){
        wstMsg('卡号格式错误', 'accNo');
        return false;
    }

    var post = {
        'id': id,
        'areaId2': areaId2,
        'accUser': accUser,
        'accNo': accNo,
        'accTargetId': accTargetId
    };
    $.post(WST.U('WebApp/Money/saveAccount'), post, function(data){
        var json = WST.toJson(data);
        if(json.status == 1){
            var msg = (id > 0) ? '保存成功' : '新增成功';
            wstMsg(msg);
            setTimeout(function(){
                location.href = WST.U('WebApp/Money/getAccountList');
            }, 2000);
        }else{
            wstMsg(json.msg);
        }
    });
}

//删除银行账户
function toDeleteAccount(accountId){
    wstConfirm('确定删除？', function(){
        deleteAccount(accountId);
    });
}

//删除银行账户
function deleteAccount(accountId){
    $.post(WST.U('WebApp/Money/deleteAccount'), {id: accountId}, function(data){
        var json = WST.toJson(data);
        if(json.status != -1){
            wstMsg('删除成功');
            setTimeout(function(){
                location.href = WST.U('WebApp/Money/getAccountList');
            },2000);
        }else{
            wstMsg('删除失败，请重试！');
        }
    });
}

$(document).ready(function(){


});